package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.Attachment;
import com.ruoyi.system.domain.CourseAttachment;
import com.ruoyi.system.domain.CourseTopic;
import com.ruoyi.system.domain.SysTopic;
import com.ruoyi.system.mapper.CourseTopicMapper;
import com.ruoyi.system.mapper.SysTopicMapper;
import com.ruoyi.system.service.ISysTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysTopicService implements ISysTopicService {

    @Autowired
    private SysTopicMapper topicMapper;

    @Resource
    private CourseTopicMapper courseTopicMapper;
    /**
     * 通过ID查询单条数据
     *
     * @param topicId 主键
     * @return 实例对象
     */
    @Override
    public SysTopic queryById(Long topicId) {
        return this.topicMapper.queryById(topicId);
    }

    @Override
    public List<SysTopic> list(SysTopic topic) {
        return topicMapper.list(topic);
    }


//    /**
//     * 分页查询
//     *
//     * @param topic      筛选条件
//     * @return 查询结果
//     */
//    @Override
//    public List<SysTopic> queryList(SysTopic topic) {
//        return topicMapper.queryAllByLimit(topic);
//    }




    /**
     * 新增数据
     * @param topic
     * @return 实例对象
     */
    @Override
    public void add(SysTopic topic){
        //测试，确保topicName不为null
        if (topic.getTopicName() == null || topic.getTopicName().trim().isEmpty()) {
            throw new IllegalArgumentException("Topic name cannot be null or empty.");
        }
        topicMapper.insert(topic);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public void delete(Integer id) {
        topicMapper.deleteById(id);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysTopic getById(Integer id) {
        return topicMapper.getById(id);
    }

    /**
     * 修改数据
     * @param topic
     * @return 实例对象
     */
    @Override
    public void update(SysTopic topic) {
        topicMapper.update(topic);
    }

    @Override
    public List<SysTopic> getTopicByCourseIdAndType(String courseId, String topicType) {
        List<CourseTopic> courseTopics = courseTopicMapper.queryAllByCourseIdAndTopicType(courseId, topicType);
        List<SysTopic> topics = new ArrayList<>();
        for (CourseTopic courseTopic : courseTopics){
            topics.add(topicMapper.queryById(courseTopic.getTopicId()));
        }
        return topics;
    }


}


